﻿using System.Data;

namespace VitCode.Castle.Helper
{
    /// <summary>
    /// DataTableCheckerHelper 的摘要說明。
    /// </summary>
    public abstract class DataTableCheckerHelper
    {
        private EmptyColumnChecker m_NotEmptyColumn;
        private RepeatColumnChecker m_NotRepeatColumn;

        /// <summary>
        /// 構建檢查器
        /// </summary>
        public DataTableCheckerHelper()
        {
            m_NotEmptyColumn = new EmptyColumnChecker(this);
            m_NotRepeatColumn = new RepeatColumnChecker(this);
        }

        /// <summary>
        /// 獲取或設置需要檢查不能為空的列的集合
        /// </summary>
        public EmptyColumnChecker NotEmptyColumns
        {
            get { return m_NotEmptyColumn; }
            set { m_NotEmptyColumn = value; }
        }

        /// <summary>
        /// 獲取或設置不能重複的列並且不能為空的列的集合
        /// </summary>
        public RepeatColumnChecker NotRepleateColumns
        {
            get { return m_NotRepeatColumn; }
            set { m_NotRepeatColumn = value; }
        }

        /// <summary>
        /// 根據從Excel生成的DataTable中的行索引，獲取
        /// Excel表格中的行數
        /// </summary>
        /// <param name="DataRowIndex"></param>
        /// <returns></returns>
        public int GetSourceDataIndex(int DataRowIndex)
        {
            if (HasTitle)
                return DataRowIndex + 2;
            else
                return DataRowIndex + 1;
        }

        /// <summary>
        /// 根據Excel表格中的行數獲取 DataTable中的行索引
        /// </summary>
        /// <param name="RowNumber"></param>
        /// <returns></returns>
        public int GetRowsIndex(int RowNumber)
        {
            if (HasTitle)
                return RowNumber - 2;
            else
                return RowNumber - 1;
        }

        private bool m_HasTitle = true;

        /// <summary>
        /// 獲取Excel檔中是否有列頭,這個屬性會影響<see cref="GetSourceDataIndex"/>和<see cref="GetRowsIndex"/>的判斷
        /// </summary>
        public virtual bool HasTitle
        {
            get { return m_HasTitle; }
            set { m_HasTitle = value; }
        }

        /// <summary>
        /// 獲取需要被檢查的DataTable
        /// </summary>
        /// <returns></returns>
        public abstract DataTable GetDataTable();

        /// <summary>
        /// 獲取需要被檢查的Title
        /// </summary>
        /// <returns></returns>
        public virtual DataColumnCollection GetCheckTitles()
        {
            return GetEmptyDataTableFromSource().Columns;
        }

        /// <summary>
        /// 從資料源中獲取一個沒有任何資料的資料源
        /// </summary>
        public abstract DataTable GetEmptyDataTableFromSource();

        /// <summary>
        /// 創建一個與資料源一樣但是沒有資料的DataTable
        /// </summary>
        /// <returns></returns>
        public virtual DataTable CreateEmptyDataTable()
        {
            return GetEmptyDataTableFromSource().Clone();
        }
    }
}
